home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer: Getting Started / Internet Surfer - Getting Started (Wayzata Technology)(7231)(1995).bin / pc / mac / bonus / peter_le / talk_sou / my_libra / myapplee.uni < prev    next >
Text File  |  1992-04-20  |  4KB  |  129 lines

  1. unit MyAppleEvents;
  2.  
  3. { This program was written by Peter N Lewis, Mar 1992 in THINK Pascal 4.0.1 }
  4.  
  5. interface
  6.  
  7.     function InitAppleEvents (DoOApp, DoODoc, DoPrint, DoQuit: Ptr): OSErr;
  8. { function DoOApp: OSErr }
  9. { function DoODoc (fs: FSSpec): OSErr }
  10. { function DoPrint (fs: FSSpec): OSErr }
  11. { function DoQuit: OSErr}
  12.  
  13. implementation
  14.  
  15.     uses
  16.         AppleTalk, PPCToolbox, Processes, EPPC, Notification, AppleEvents, MyUtilities;
  17.  
  18.     function DoOApp (p: ptr): OSErr;
  19.     inline
  20.         $205F, $4E90;
  21.  
  22.     function DoDocs (fs: FSSpec; p: ptr): OSErr;
  23.     inline
  24.         $205F, $4E90;
  25.  
  26.     function DoQuit (p: ptr): OSErr;
  27.     inline
  28.         $205F, $4E90;
  29.  
  30.     const
  31.         kPatienceLevel = 1000;                                { <aevt> ticks we wait for response }
  32.         kSysEnvironsVersion = 1;
  33.         kOSEvent = app4Evt;    {event used by MultiFinder}
  34.         kSuspendResumeMessage = 1;        {high byte of suspend/resume event message}
  35.         kResumeMask = 1;        {bit of message field for resume vs. suspend}
  36.         kMouseMovedMessage = $FA;        {high byte of mouse-moved event message}
  37.         kNoEvents = 0;        {no events mask}
  38.         kNoListChosen = -1;
  39.  
  40.     function GotRequiredParams (theAppleEvent: AppleEvent): OSErr;        { <aevt> }
  41.         var
  42.             typeCode: DescType;
  43.             actualSize: Size;
  44.             err: OSErr;
  45.     begin
  46.         err := AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, typeCode, nil, 0, actualSize);    { nil ok: need only function result }
  47.         if err = errAEDescNotFound then        { we got all the required params: all is ok }
  48.             GotRequiredParams := noErr
  49.         else if err = noErr then
  50.             GotRequiredParams := errAEEventNotHandled
  51.         else
  52.             GotRequiredParams := err;
  53.     end; { GotRequiredParams }
  54.  
  55.     function HandleOAPP (theAppleEvent, reply: AppleEvent; openappp: ptr): OSErr;{ <aevt> }
  56.         var
  57.             oe: OSErr;
  58.     begin
  59.     { We don't expect any params at all, but check in case the client requires any }
  60.         oe := GotRequiredParams(theAppleEvent);
  61.         oe := DoOApp(openappp);
  62.         HandleOAPP := oe;
  63.     end;
  64.  
  65.     function HandleDocs (theAppleEvent, reply: AppleEvent; dodocp: ptr): OSErr;        { <aevt> }
  66.         var
  67.             myFSS: FSSpec;
  68.             docList: AEDescList;
  69.             index, itemsInList: LONGINT;
  70.             actualSize: Size;
  71.             keywd: AEKeyword;
  72.             typeCode: descType;
  73.             ignoreWPtr: WindowPtr;
  74.             oe, ooe: OSErr;
  75.     begin
  76.         oe := AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, docList);
  77.         if oe = noErr then begin
  78.             ooe := GotRequiredParams(theAppleEvent);
  79.     { now get each alias from the list (as an FSSSpec) and open the associated file. }
  80.             oe := AECountItems(docList, itemsInList);
  81.             for index := 1 to itemsInList do begin
  82.                 ooe := AEGetNthPtr(docList, index, typeFSS, keywd, typeCode, @myFSS, sizeof(myFSS), actualSize);
  83. { coercion does alias->fsspec }
  84.                 if ooe = noErr then
  85.                     ooe := DoDocs(myFSS, dodocp);
  86.             end;
  87.             ooe := AEDisposeDesc(docList);
  88.         end;
  89.         HandleDocs := oe;
  90.     end; { HandleDocs }
  91.  
  92.     function HandleQUIT (theAppleEvent, reply: AppleEvent; quitp: ptr): OSErr;        { <aevt> }
  93.         var
  94.             oe: OSErr;
  95.             errStr: Str255;
  96.             willQuit: Boolean;                { did the user allow the quit or cancel }
  97.     begin
  98.     { We don't expect any params at all, but check in case the client requires any }
  99.         oe := GotRequiredParams(theAppleEvent);
  100.         oe := DoQuit(quitp);            { set global boolean: app will exit at end of event loop }
  101.         if reply.dataHandle <> nil then            { a reply is sought }
  102.             begin
  103.             if oe = noErr then
  104.                 errStr := 'OK'
  105.             else
  106.                 errStr := 'user cancelled quit';
  107.             oe := AEPutParamPtr(reply, 'errs', 'TEXT', Ptr(@errStr[1]), length(errStr));
  108.         end;
  109.         HandleQUIT := oe;
  110.     end;
  111.  
  112. {$S Init}
  113.     function InitAppleEvents (DoOApp, DoODoc, DoPrint, DoQuit: Ptr): OSErr;
  114.         var
  115.             aevtErr: OSErr;
  116.     begin
  117.         aevtErr := noErr;
  118.         if (aevtErr = noErr) and (DoOApp <> nil) then
  119.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, @HandleOAPP, longInt(DoOApp), false);
  120.         if (aevtErr = noErr) and (DoODoc <> nil) then
  121.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, @HandleDocs, longInt(DoODoc), false);
  122.         if (aevtErr = noErr) and (DoPrint <> nil) then
  123.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, @HandleDocs, longInt(DoPrint), false);
  124.         if (aevtErr = noErr) and (DoQuit <> nil) then
  125.             aevtErr := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, @HandleQUIT, longInt(DoQuit), false);
  126.         InitAppleEvents := aevtErr;
  127.     end;
  128.  
  129. end.